package com.wqr.controller;



import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wqr.exception.MD5Util;
import com.wqr.pojo.Login;
import com.wqr.pojo.PasswordInfo;
import com.wqr.service.LoginService;
import com.wqr.service.PasswordInfoService;

@Controller
public class ModifyPasswordController {
	@Autowired
    public PasswordInfoService passwordInfoService;
	@Autowired
    public LoginService loginService;
	
	@RequestMapping("/modifyPassword")
	public String passwordInfo() throws Exception {
	    return "modifyPassword"; 
	}
	
	@RequestMapping("/updatePassword")
	public String updatePassword(String txtOldPassword,String txtNewPassword,HttpServletRequest request,
			PasswordInfo passwordInfo,Login login,HttpSession session) throws Exception {
		login = (Login)session.getAttribute("login");
		txtOldPassword = MD5Util.md5Encode(txtOldPassword.trim());
		txtNewPassword = MD5Util.md5Encode(txtNewPassword.trim());
		
		//旧密码和数据库的进行比较
		if (login != null && login.getPassword().compareTo(txtOldPassword) == 0) {
			login.setPassword(txtNewPassword);
		}else {
			request.setAttribute("alertMessage",getAlertScript("旧密码输入错误！"));
			return "modifyPassword"; 
		}
		//修改密码
		
		int a = loginService.updateLogin(login);
		if(a != 0) {
			request.setAttribute("alertMessage", getAlertScript("密码已修改，下次登陆时生效！"));
		}else {
			request.setAttribute("alertMessage", getAlertScript("密码修改失败！"));
		}
		//添加修改密码日志
		passwordInfo.setOldPassword(txtOldPassword);
		passwordInfo.setNewPassword(txtNewPassword);
		passwordInfo.setLoginId(login.getLoginId());
		passwordInfo.setModifyTime(new Date());
		passwordInfo.setLoginName(login.getLoginName());
		
		int b = passwordInfoService.insertPasswordInfo(passwordInfo);
		if(b == 0) {
			request.setAttribute("alertMessage", getAlertScript("添加密码修改日志失败！"));
		}
	    return "modifyPassword"; 
	}
	@RequestMapping("/userUpdatePassword")
	public String userUpdatePassword(String txtOldPassword,String txtNewPassword,HttpServletRequest request,
			PasswordInfo passwordInfo,Login login,HttpSession session) throws Exception {
		login = (Login)session.getAttribute("login");
		txtOldPassword = MD5Util.md5Encode(txtOldPassword.trim());
		txtNewPassword = MD5Util.md5Encode(txtNewPassword.trim());
		
		//旧密码和数据库的进行比较
		if (login != null && login.getPassword().compareTo(txtOldPassword) == 0) {
			login.setPassword(txtNewPassword);
		}else {
			request.setAttribute("alertMessage",getAlertScript("旧密码输入错误！"));
			return "userModifyPassword"; 
		}
		//修改密码
		
		int a = loginService.updateLogin(login);
		if(a != 0) {
			request.setAttribute("alertMessage", getAlertScript("密码已修改，下次登陆时生效！"));
		}else {
			request.setAttribute("alertMessage", getAlertScript("密码修改失败！"));
		}
		//添加修改密码日志
		passwordInfo.setOldPassword(txtOldPassword);
		passwordInfo.setNewPassword(txtNewPassword);
		passwordInfo.setLoginId(login.getLoginId());
		passwordInfo.setModifyTime(new Date());
		passwordInfo.setLoginName(login.getLoginName());
		
		int b = passwordInfoService.insertPasswordInfo(passwordInfo);
		if(b == 0) {
			request.setAttribute("alertMessage", getAlertScript("添加密码修改日志失败！"));
		}
	    return "userModifyPassword"; 
	}
	static public String getAlertScript(String message) {
		if (message == null)
			message = "";
		return "alert('" + message.replace("'", "\"").replace("\n", "") + "');";
	}
	
}
